﻿using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Managers.ResourceManagerV2.Runtime;

public class CharacterTextureCtrl : MonoBehaviour
{
    //贴花槽编号
    public enum IndexType
    {
        EYEBROW = 1,
        BEARD = 2,
    }
    
    //用于记录槽Shader logo属性
    private Dictionary<IndexType,string> textureIndexes = new Dictionary<IndexType, string>
    {
        {IndexType.EYEBROW, "_LogoTex"},
        {IndexType.BEARD, "_LogoTex02"},
    };
    private Dictionary<IndexType,string> areaIndexes = new Dictionary<IndexType, string>
    {
        {IndexType.EYEBROW, "_LogoArea"},
        {IndexType.BEARD, "_LogoArea02"},
    };
    //用于记录槽Shader color属性
    private Dictionary<IndexType,string> colorIndexes = new Dictionary<IndexType, string>
    {
        {IndexType.EYEBROW, "_LogoColor"},
        {IndexType.BEARD, "_LogoColor02"},
    };
    //用于记录槽应用的贴花
    private Dictionary<IndexType,Texture2D> indexTexes = new Dictionary<IndexType, Texture2D>();
    //用于记录槽应用的贴花颜色
    private Dictionary<IndexType,Color> indexColors = new Dictionary<IndexType, Color>();
    
    private ResLoader m_resLoader;

    /// <summary>
    /// 设置纹理贴花
    /// </summary>
    /// <param name="index">贴花槽编号</param>
    /// <param name="path">贴花资源路径</param>
    public void SetClothLogoTex(int index, string path)
    {
        //加载资源
        if (m_resLoader == null)
            m_resLoader = ResLoader.Alloc();

        m_resLoader.Add2Load(path, (success, assetName, asset) =>
        {
            if (success && asset is Texture2D tex)
            {
                IndexType key = (IndexType)index;
                if (indexTexes.ContainsKey(key))
                    indexTexes[key] = tex;
                else
                    indexTexes.Add(key, tex);

                UpdateMaterialTexture(key);
            }
        });
    }

    /// <summary>
    /// 设置纹理贴花的颜色
    /// </summary>
    /// <param name="index">贴花槽编号</param>
    /// <param name="color">贴花颜色值</param>
    public void SetClothLogoColor(int index, Color color)
    {
        //需要保证已经有贴图了才行
        IndexType key = (IndexType) index;
        if (indexTexes.ContainsKey(key))
        {
            if (indexColors.ContainsKey(key))
                indexColors[key] = color;
            else
                indexColors.Add(key, color);
            
            UpdateMaterialTexture(key);
        }
    }

    /// <summary>
    /// 同时更新纹理贴花图案和颜色
    /// </summary>
    /// <param name="index"></param>
    /// <param name="path"></param>
    /// <param name="color"></param>
    public void SetClothLogoAndColor(int index, string path, Color color)
    {
        //加载资源
        if (m_resLoader == null)
            m_resLoader = ResLoader.Alloc();
        
        Debug.Log("C# [CharacterTextureCtrl] SetClothLogoAndColor " + index);

        m_resLoader.Add2Load(path, (success, assetName, asset) =>
        {
            if (success && asset is Texture2D tex)
            {
                IndexType key = (IndexType)index;
                if (indexTexes.ContainsKey(key))
                    indexTexes[key] = tex;
                else
                    indexTexes.Add(key, tex);

                if (indexColors.ContainsKey(key))
                    indexColors[key] = color;
                else
                    indexColors.Add(key, color);

                UpdateMaterialTexture(key);
            }
            else
            {
                Debug.LogError("C# [CharacterTextureCtrl] SetClothLogoAndColor failed, tex is null");
            }
        });
    }

    /// <summary>
    /// 清除纹理贴花
    /// </summary>
    /// <param name="index">贴花槽编号</param>
    public void RemoveClothLogoTex(int index)
    {
        IndexType key = (IndexType) index;
        SkinnedMeshRenderer renderer = GetComponent<SkinnedMeshRenderer>();
        Material mat = renderer.materials[0];
        
        string texName = textureIndexes[key];
        mat.SetTexture(texName, null);
        string areaName = areaIndexes[key];
        mat.SetVector(areaName, new Vector4(-2, -2, -1, -1));
    }

    /// <summary>
    /// 更新材质纹理贴花属性
    /// </summary>
    /// <param name="index">贴花槽编号</param>
    private void UpdateMaterialTexture(IndexType index)
    {
        SkinnedMeshRenderer renderer = GetComponent<SkinnedMeshRenderer>();
        Material mat = renderer.materials[0];
        
        Texture2D tex = indexTexes[index];
        string texName = textureIndexes[index];
        if (tex != null && texName != null)
        {
            //设置纹理
            mat.SetTexture(texName, tex);

            //设置颜色
            Color color = indexColors[index];
            string colorName = colorIndexes[index];
            if (color != null && colorName != null)
                mat.SetColor(colorName, color);
            
            //设置UV值
            string areaName = areaIndexes[index];
            mat.SetVector(areaName, new Vector4(0, 0, 1, 1));
        }
    }

    void OnDestroy()
    {
        m_resLoader?.Recycle2Cache();
        m_resLoader = null;
    }
}
